<template>
    <div class="page-release">
        <release-list v-if="this.$store.state.userInfo" :ReleaseList="ReleaseList"></release-list>
        <release-empty text="暂无草稿文章,快去新建吧" v-if="!this.$store.state.userInfo || showEmpty" :showEmpty="showEmpty"></release-empty>
        <release-toast :loading="toasts.loading" :toast="toasts.toast" :text="toasts.text" :icon="toasts.icon" :iconfont="toasts.iconfont"></release-toast>
    </div>
</template>

<script>
import ReleaseList from '../components/Release/List/Index'
import ReleaseEmpty from '../components/Common/Empty/Index'
import ReleaseToast from '../components/Common/Toast/Index'
export default {
    name: 'PageRelease',
    components: {
        ReleaseList,
        ReleaseEmpty,
        ReleaseToast
    },
    data() {
        return {
            ReleaseList: [],
            showEmpty: false,

            toasts: {
                loading: false,
                toast: false,
                text: '',
                icon: false,
                iconfont: ''
            }
        }
    },
    methods: {
        getRelease() {
            if(this.$store.state.userInfo) {
                this.ToastLoading()
                this.$post('users/list.php',{
                    id: this.$store.state.userInfo.id
                })
                .then((res) => {
                    if(res.code == 400) {
                        this.Toast(`${res.desc}`,'icon-meh',800)
                        this.$store.commit('setUserInfo','')
                    }else {
                        this.ReleaseList = res
                        this.ToastClear()
                        if(res.length == 0) {
                            this.showEmpty = true
                        }
                    }
                })
            }
        },

        Toast(text,iconfont,time) {
            this.toasts.loading = false
            this.toasts.text = text
            this.toasts.icon = true
            this.toasts.iconfont = iconfont
            this.toasts.toast = true
            window.setTimeout(() => {
                this.toasts.toast = false
            },time);
        },
        ToastLoading() {
            this.toasts.loading = true
            this.toasts.icon = false
            this.toasts.text = '加载中...'
            this.toasts.toast = true
        },
        ToastClear() {
            this.toasts.toast = false
        }
    },
    created() {
        this.getRelease()
    }
}
</script>

<style scoped>
.page-release{
    width:100vw;
    height:100vh;
    overflow: auto;
    background:#fff;
    position: fixed;
}
</style>